// import {decodeURIWrapper, encodeURIWrapper, getHighlightStr} from "./common.js";

import {decodeURIWrapper, encodeURIWrapper, getHighlightStr} from "~utils/common";

const composeHighlightFromViewstate = (viewstate) => {
  /* 两种viewstate，如何才能正确区分？看来还是加一个ver比较好 TODO: 用ver来区分两种不同viewstate
  1. 2d3a4431-c7f2-4d70-af12-57fcd1086e7f;;#post-33838>div:nth-of-type(1)>ul:nth-of-type(2);;>li:nth-of-type(3)>textNode:nth-of-type(0);;>li:nth-of-type(4)>textNode:nth-of-type(0);;1;;34;;00% Lossless & MD5 Perfect: all files are identical to originals after installation
NOTHING ripped, NOTHING re-encoded;;
  2. 0360afc6-ef28-4842-b38a-0c3c80106924;;Significantly smaller archive size (compressed from 832 to -,550 MB,-)
;;
   */
  if (viewstate.indexOf("*ver_simple*") !== -1) {
    const tmp = viewstate.split(";;");
    const uuid = tmp[0].trim()
    const contextText = decodeURIWrapper(tmp[1].trim());
    const selectionText = getHighlightStr(contextText);
    let combinedColor = decodeURIWrapper(tmp[2].trim());
    let color = combinedColor;
    let textColor = undefined;
    if (combinedColor.indexOf("&&") !== -1) { // JM：表示有textColor
      color = combinedColor.split("&&")[0];
      textColor = combinedColor.split("&&")[1];
    }
    const ver = tmp[3].trim();
    const comments = decodeURIWrapper(tmp[4].trim());
    return {
      uuid: uuid,
      context: contextText,
      string: selectionText,
      color: color,
      textColor: textColor,
      ver: ver,
      comments: comments,
      href: location.href,
      viewstate: viewstate,  // JM: 这里需要将原始的用来定位的hash注入页面
    };
  } else if (viewstate.indexOf("*ver_image*") !== -1) {
    const tmp = viewstate.split(";;");
    return {
      uuid: tmp[0],
      imageName: decodeURIWrapper(tmp[1]),
      imageSrc: decodeURIWrapper(tmp[2]),
      color: decodeURIWrapper(tmp[3]),
      ver: tmp[4],
      comments: decodeURIWrapper(tmp[5]),
      viewstate: viewstate,  // JM: 这里需要将原始的用来定位的hash注入页面
    };
  } else if (viewstate.indexOf("*ver_page*") !== -1) {
    const tmp = viewstate.split(";;");
    const uuid = tmp[0].trim();
    let combinedColor = decodeURIWrapper(tmp[1].trim());
    let color = combinedColor;
    let textColor = undefined;
    if (combinedColor.indexOf("&&") !== -1) { // JM：表示有textColor
      color = combinedColor.split("&&")[0];
      textColor = combinedColor.split("&&")[1];
    }
    const ver = tmp[2].trim();
    const comments = decodeURIWrapper(tmp[3].trim());
    return {
      uuid: uuid,
      color: color,
      textColor: textColor,
      ver: ver,
      comments: comments,
      viewstate: viewstate,  // JM: 这里需要将原始的用来定位的hash注入页面
    };
  } else {
    const tmp = viewstate.split(";;");
    const uuid = tmp[0].trim();
    const container = decodeURIWrapper(tmp[1]);
    const anchorNode = decodeURIWrapper(tmp[2]).replace("_RC_", decodeURIWrapper(tmp[1]));
    const focusNode = decodeURIWrapper(tmp[3]).replace("_RC_", decodeURIWrapper(tmp[1]));
    const anchorOffset = tmp[4].trim();
    const focusOffset = tmp[5].trim();
    const string = decodeURIWrapper(tmp[6].trim());
    let combinedColor = decodeURIWrapper(tmp[7].trim());
    let color = combinedColor;
    let textColor = undefined;
    if (combinedColor.indexOf("&&") !== -1) { // JM：表示有textColor
      color = combinedColor.split("&&")[0];
      textColor = combinedColor.split("&&")[1];
    }
    const ver = tmp[8].trim();
    const comments = decodeURIWrapper(tmp[9].trim());
    return {
      uuid: uuid,
      container: container,
      anchorNode: anchorNode,
      focusNode: focusNode,
      anchorOffset: anchorOffset,
      focusOffset: focusOffset,
      string: string,
      color: color,
      textColor: textColor,
      ver: ver,
      comments: comments,
      viewstate: viewstate,  // JM: 这里需要将原始的用来定位的hash注入页面
    };
  }
}

const stringifyHighlighsToViewstate = (highlight) => {
  if (highlight.ver === "*ver_simple*") {
    const uuid = highlight.uuid;
    // const string = highlight.string;
    const context = highlight.context;
    const color = highlight.color;
    const textColor = highlight.textColor;
    const ver = highlight.ver;
    const comments = highlight.comments.trim();//.replaceAll("\n", "%0A");
    let combinedColor = color;
    if (textColor) {
      combinedColor = `${color}&&${textColor}`;
    }
    const viewstate = `${uuid};;${encodeURIWrapper(context)};;${encodeURIWrapper(combinedColor)};;${ver};;${encodeURIWrapper(comments)}`;
    // const encodedViewstate = encodeURIWrapper(viewstate);
    // navigator.clipboard.writeText(encodedViewstate);
    // console.log(viewstate);
    // console.log(encodedViewstate);
    return viewstate;
  } else if (highlight.ver === "*ver_image*") {
    const uuid = highlight.uuid;
    const imageSrc = highlight.imageSrc;
    const imageName = highlight.imageName;
    const color = highlight.color;
    const ver = highlight.ver;
    const comments = highlight.comments.trim();//.replaceAll("\n", "%0A");
    const viewstate = `${uuid};;${encodeURIWrapper(imageName)};;${encodeURIWrapper(imageSrc)};;${encodeURIWrapper(color)};;${ver};;${encodeURIWrapper(comments)}`;
    // const encodedViewstate = encodeURIWrapper(viewstate);
    // console.log(viewstate);
    // console.log(encodedViewstate);
    return viewstate;
  } else if (highlight.ver === "*ver_page*") {
    const uuid = highlight.uuid;
    const color = highlight.color;
    const textColor = highlight.textColor;
    const ver = highlight.ver;
    const comments = highlight.comments.trim();//.replaceAll("\n", "%0A");
    let combinedColor = color;
    if (textColor) {
      combinedColor = `${color}&&${textColor}`;
    }
    const viewstate = `${uuid};;${encodeURIWrapper(combinedColor)};;${ver};;${encodeURIWrapper(comments)}`;
    // const encodedViewstate = encodeURIWrapper(viewstate);
    // console.log(viewstate);
    // console.log(encodedViewstate);
    return viewstate;
  }
  else {
    const findUnicodeRegex = /([\uD800-\uDBFF])[^\uDC00-\uDFFF]|^([\uDC00-\uDFFF])|([\uD800-\uDBFF])$|[^\uD800-\uDBFF]([\uDC00-\uDFFF])/g;
    /**
     * 查找高低位不完整的字符编码位置
     * @param {string} str
     * @returns {number}
     */
    function findInvalidUnicode (str) {
      const reg = findUnicodeRegex.exec(str);
      if (reg) {
        const index = reg.index;
        return reg[4] ? index + 1 : index;
      }
      return -1;
    }

    const invalidCharStartIndex = findInvalidUnicode(highlight.string);
    if (invalidCharStartIndex !== -1){
      // highlight.string = highlight.string.substring(0, invalidCharStartIndex);
      highlight.string = highlight.string.replace(findUnicodeRegex, "");
    }

    const uuid = highlight.uuid;
    const containerQuery = highlight.container;
    const anchorNodeQuery = highlight.anchorNode.replace(containerQuery, "_RC_");
    const focusNodeQuery = highlight.focusNode.replace(containerQuery, "_RC_");
    const anchorOffset = highlight.anchorOffset;
    const focusOffset = highlight.focusOffset;
    const string = highlight.string;
    const color = highlight.color;
    const textColor = highlight.textColor;
    const ver = highlight.ver;
    const comments = highlight.comments.trim();//.replaceAll("\n", "%0A");
    let combinedColor = color;
    if (textColor) {
      combinedColor = `${color}&&${textColor}`;
    }

    const viewstate = `${uuid};;${encodeURIWrapper(containerQuery)};;${encodeURIWrapper(anchorNodeQuery)};;${encodeURIWrapper(focusNodeQuery)};;${anchorOffset};;${focusOffset};;${encodeURIWrapper(string)};;${encodeURIWrapper(combinedColor)};;${ver};;${encodeURIWrapper(comments)}`;
    // const encodedViewstate = encodeURI(viewstate.trim()).replaceAll("#", "%23").replaceAll(":", "%3A").replaceAll("(", "%28").replaceAll(")", "%29").replaceAll("\n", "%0A");
    // const encodedViewstate = encodeURIWrapper(viewstate);
    // navigator.clipboard.writeText(encodedViewstate);
    // console.log(viewstate);
    // console.log(encodedViewstate);
    return viewstate;
  }
}

export {
  composeHighlightFromViewstate,
  stringifyHighlighsToViewstate
}
